<?php   
    require_once("config.php");
    
    if ($_POST) {
        // then connect to the other database
        $oc1 = mysql_connect("db","offcampus","urte9834");
        mysql_select_db("offcampus",$oc1);
        
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $query = "SELECT * FROM landlord WHERE username LIKE '$username' AND password='$password';";
        $result = mysql_query($query,$oc1) or die("ERROR: " . mysql_error());
        
        if (mysql_num_rows($result) > 0) {
            $row = mysql_fetch_assoc($result);
            $email = $_POST['email'];
            if (empty($email)) $email = $row['email'];
            if (empty($email)) {
                header("Location: migrate.php?error=noemail");
                exit;
            }
            
            $build = array(
                "email" => $email,
                "password" => md5($password),
                "name" => $row['name'],
                "created" => time(),
                "expires" => 0,
                "override_auth" => AuthConstants::LANDLORDS
                "approved" => 1
            );
            $query = QueryTools::CreateQuery($build,"guests","insert");
            DBM::Execute($query);
            
            Functions::PrintHeader("Success!");
            SuccessMessage::Show("Success!","Your account has been transferred.  You may now log in <a href='login.php'>here</a> using <b>$email</b> as your E-Mail address.");
            Functions::PrintFooter();
        }
        else header("Location: migrate.php?error=notfound");
    }
    else {
        Functions::PrintHeader("Transfer Account");
        
        if ($_GET['error'] == "noemail") {
            ErrorMessage::Show("No E-Mail","We couldn't find an E-Mail address on file.  Please specify one and try again.");
        }
        if ($_GET['error'] == "notfound") {
            ErrorMessage::Show("Invalid User","Your username and password combination is incorrect.  Please try again.");
        }
        
        ?>
        <h2>Landlords: Migrate your account from the old offcampus site</h2>
        This form will transfer your name and login information from the old offcampus site.  This will give you access to the new site
        without having to go through the signup process.  Please note that <b>this will not transfer your listings</b>.  Due to compatibility problems
        and our desire to keep the new system as up to date as possible, we are asking that you re-list any properties that you may have
        put up for rent.
        <br />
        <br />
        <h2>Notice:</h2>
        Migrating your account still binds you to the same terms of service and privacy statements as the old offcampus housing website.  The 
        documents you have agreed to already are listed below.  By migrating your account, you accept these terms in the new system as well.<br />
        <ul>
            <li><a target="_blank" href='tos.php'>The Terms of Service</a></li>
            <li><a target="_blank" href='privacy.php'>The Privacy Statement</a></li>
            <li><a target="_blank" href='fairhousing.php'>The Fair and Equal Housing Policy</a></li>
        </ul>
        <br />
        <br />
        <h2>Get Started</h2>
        <form method="POST" action="migrate.php">
            <table class='cgltable'>
                <tr>
                    <th colspan='2'>Migrate Your Old Account</th>
                </tr>
                <tr>
                    <td class='label'>Username</td>
                    <td><input type='text' name='username' /></td>
                </tr>
                <tr>
                    <td class='label'>E-Mail Address</td>
                    <td>
                        <input type='text' name='email' /><br />
                        <span style='font-size:8pt;'>
                            Your E-Mail address in the new system doubles as your username.  <br />
                            If you haven't specified an E-Mail in the old system or you'd like <br />
                            to specify a different one, then type it in this field.  If you want <br />
                            to use your E-Mail address from the old system, just leave it blank.
                        </span>
                    </td>
                </tr>
                <tr>
                    <td class='label'>Password</td>
                    <td><input type='password' name='password' /></td>
                </tr>
                <tr>
                    <td colspan='2'>
                        <input type='submit' value='Start Migration' />
                    </td>
                </tr>
            </table>
        </form>
        <?php
        Functions::PrintFooter();
    }
?>
